package com.yc.java.io;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class changeFileName extends JFrame implements ActionListener,
		ItemListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8362077414436894016L;

	private JButton btnStart, btnOpen;
	private ButtonGroup bgReBound;
	private JComboBox cbSelcetModel;
	private JPanel[] panelSet = new JPanel[5];
	private JTextField tfFilePath, tfExt, tfSource, tfReplace, tfPrefix,
			tfSuffix, tfStartNumber, tfAddSize;
	private JRadioButton rbReCurrentDirectory, rbReAllDirectory, rbToLower,
			rbToUpper, rbOneUpper, rbNumber, rbPrefix, rbSuffix;
	private JLabel lbStatus, lbReModel, lbStartNumber, lbAddSize;
	private JFileChooser chooser;

	public changeFileName() {

		this.setTitle("RenameTools v1.0 by ddk ");

		this.setLayout(null);
		// this.getRootPane().setBorder(new TitledBorder("dafds"));
		btnOpen = new JButton("打开:");
		btnOpen.setBounds(12, 15, 65, 25);
		this.add(btnOpen);

		tfFilePath = new JTextField();
		tfFilePath.setBounds(80, 15, 250, 25);
		this.add(tfFilePath);

		bgReBound = new ButtonGroup();
		rbReAllDirectory = new JRadioButton("下级目录");
		rbReCurrentDirectory = new JRadioButton("当前目录");
		rbReCurrentDirectory.setSelected(true);
		rbReAllDirectory.setBounds(12, 50, 100, 30);
		rbReCurrentDirectory.setBounds(130, 50, 100, 30);
		this.add(rbReAllDirectory);
		this.add(rbReCurrentDirectory);

		bgReBound.add(rbReAllDirectory);
		bgReBound.add(rbReCurrentDirectory);

		btnStart = new JButton("开始");
		btnStart.setBounds(265, 50, 65, 25);
		this.add(btnStart);

		lbReModel = new JLabel();
		lbReModel.setText("更名方式:");
		lbReModel.setBounds(12, 80, 55, 25);
		this.add(lbReModel);

		cbSelcetModel = new JComboBox();
		cbSelcetModel.setBounds(80, 80, 250, 25);

		cbSelcetModel.addItem("大小写转换");
		cbSelcetModel.addItem("更改扩展名");
		cbSelcetModel.addItem("替换文件名");
		cbSelcetModel.addItem("前缀和后缀");

		cbSelcetModel.addItemListener(this);
		this.add(cbSelcetModel);

		lbStatus = new JLabel();
		lbStatus.setBounds(12, 230, 320, 25);
		lbStatus.setBorder(BorderFactory.createTitledBorder(""));
		this.add(lbStatus);

		btnOpen.addActionListener(this);
		btnStart.addActionListener(this);

		panelSet[0] = lowerToupper();
		panelSet[1] = reSuffix();
		panelSet[2] = replaceFileName();
		panelSet[3] = prefixAndsuffix();
		// panelSet[0] = lowerToupper();

		setPanel(0);

		chooser = new JFileChooser();
		chooser.setCurrentDirectory(new File("."));
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);// 设定只能选择目录模式
		// chooser.setAcceptAllFileFilterUsed(false); // 不出现所有文件选项
		chooser.setMultiSelectionEnabled(false); // 设定不能多选
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(new Dimension(350, 300));
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		int sw = toolkit.getScreenSize().width;
		int sh = toolkit.getScreenSize().height;
		int fw = this.getWidth();
		int fh = this.getHeight();
		this.setLocation((sw - fw) / 2, (sh - fh) / 2);
		this.setResizable(false);
		this.setVisible(true);
	}

	public void setPanel(int i) {
		panelSet[i].setBorder(new TitledBorder(""));
		panelSet[i].setBounds(12, 120, 320, 100);
		switch (i) {
		case 0:
			panelSet[3].setVisible(false);
			panelSet[2].setVisible(false);
			panelSet[1].setVisible(false);
			panelSet[0].setVisible(true);
			this.add(panelSet[0]);
			break;
		case 1:
			panelSet[3].setVisible(false);
			panelSet[2].setVisible(false);
			panelSet[0].setVisible(false);
			panelSet[1].setVisible(true);
			this.add(panelSet[1]);
			break;
		case 2:
			panelSet[3].setVisible(false);
			panelSet[1].setVisible(false);
			panelSet[0].setVisible(false);
			panelSet[2].setVisible(true);
			this.add(panelSet[2]);
			break;
		case 3:
			panelSet[3].setVisible(true);
			panelSet[1].setVisible(false);
			panelSet[0].setVisible(false);
			panelSet[2].setVisible(false);
			this.add(panelSet[3]);
			break;

		default:
			break;
		}

	}

	public JPanel lowerToupper() {
		JPanel lowerToupper = new JPanel();
		rbToLower = new JRadioButton("文件名大写");

		rbToUpper = new JRadioButton("文件名小写");
		rbOneUpper = new JRadioButton("头字符大写");
		ButtonGroup bgLowerToUpper = new ButtonGroup();
		bgLowerToUpper.add(rbToLower);
		bgLowerToUpper.add(rbToUpper);
		bgLowerToUpper.add(rbOneUpper);
		// rbOneUpper.setBounds(x, y, width, height)
		lowerToupper.add(rbToLower);
		lowerToupper.add(rbToUpper);
		lowerToupper.add(rbOneUpper);
		rbToUpper.setSelected(true);
		rbToLower.addActionListener(this);
		rbToUpper.addActionListener(this);
		rbOneUpper.addActionListener(this);

		return lowerToupper;
	}

	public JPanel reSuffix() {
		JPanel reSuffix = new JPanel();
		reSuffix.setLayout(null);
		JLabel lbExt = new JLabel("更改为:");

		lbExt.setBounds(12, 20, 50, 25);
		tfExt = new JTextField();
		tfExt.setBounds(70, 20, 150, 25);
		reSuffix.add(lbExt);
		reSuffix.add(tfExt);
		return reSuffix;
	}

	public JPanel replaceFileName() {
		JPanel replaceFileName = new JPanel();
		replaceFileName.setLayout(null);
		JLabel lbSource = new JLabel("将原文件名:");
		lbSource.setBounds(12, 20, 70, 25);
		tfSource = new JTextField();
		tfSource.setBounds(80, 20, 100, 25);
		JLabel lbReplace = new JLabel("替换为:");
		lbReplace.setBounds(180, 20, 50, 25);
		tfReplace = new JTextField();
		tfReplace.setBounds(230, 20, 80, 25);
		replaceFileName.add(lbSource);
		replaceFileName.add(tfSource);
		replaceFileName.add(lbReplace);
		replaceFileName.add(tfReplace);
		return replaceFileName;
	}

	/**
	 * @return
	 */
	public JPanel prefixAndsuffix() {
		JPanel prefixAndsuffix = new JPanel();
		prefixAndsuffix.setLayout(null);
		JLabel lbPrefix = new JLabel("添加前缀:");
		lbPrefix.setBounds(25, 10, 60, 25);
		tfPrefix = new JTextField();
		tfPrefix.setBounds(80, 10, 110, 25);
		JLabel lbSuffix = new JLabel("添加后缀:");
		lbSuffix.setBounds(25, 40, 60, 25);
		tfSuffix = new JTextField();
		tfSuffix.setBounds(80, 40, 110, 25);
		rbNumber = new JRadioButton("以数字命名");
		rbNumber.setBounds(200, 8, 100, 25);

		rbPrefix = new JRadioButton();
		rbPrefix.setBounds(2, 15, 20, 15);
		rbPrefix.setVisible(false);
		rbSuffix = new JRadioButton();
		rbSuffix.setBounds(2, 45, 20, 15);
		rbSuffix.setVisible(false);

		lbStartNumber = new JLabel("起始:");
		lbAddSize = new JLabel("增量:");
		lbStartNumber.setVisible(false);
		lbAddSize.setVisible(false);
		lbStartNumber.setBounds(12, 70, 60, 20);
		lbAddSize.setBounds(160, 70, 60, 20);

		tfStartNumber = new JTextField();
		tfAddSize = new JTextField();
		tfStartNumber.setVisible(false);
		tfAddSize.setVisible(false);
		tfStartNumber.setBounds(50, 70, 100, 25);
		tfAddSize.setBounds(200, 70, 100, 25);

		prefixAndsuffix.add(rbPrefix);
		prefixAndsuffix.add(rbSuffix);
		prefixAndsuffix.add(lbPrefix);
		prefixAndsuffix.add(tfPrefix);
		prefixAndsuffix.add(lbSuffix);
		prefixAndsuffix.add(tfSuffix);
		prefixAndsuffix.add(rbNumber);
		prefixAndsuffix.add(lbStartNumber);
		prefixAndsuffix.add(tfStartNumber);
		prefixAndsuffix.add(tfAddSize);
		prefixAndsuffix.add(lbAddSize);

		rbNumber.addActionListener(this);
		rbPrefix.addActionListener(this);
		rbSuffix.addActionListener(this);
		return prefixAndsuffix;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent a) {
		if (a.getSource() == btnOpen) {
			if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
				tfFilePath.setText(chooser.getSelectedFile().toString());

			}

		} else if (a.getSource() == btnStart) {
			if (tfFilePath.getText().trim().isEmpty()) {
				lbStatus.setText("源地址为空!");
			} else {
				if (!isExists(tfFilePath.getText().trim())) {
					lbStatus.setText("源地址无效!");
				} else {

					lbStatus.setText("有"
							+ getFilesCount(new File(tfFilePath.getText()
									.trim())) + "个文件");

					rename(tfFilePath.getText().trim());

				}
			}
		} else if (a.getSource() == rbNumber) {
			if (rbNumber.isSelected()) {
				rbPrefix.setVisible(true);
				rbSuffix.setVisible(true);
				lbStartNumber.setVisible(true);
				lbAddSize.setVisible(true);
				tfStartNumber.setVisible(true);
				tfAddSize.setVisible(true);

			} else {
				rbPrefix.setVisible(false);
				rbSuffix.setVisible(false);
				lbStartNumber.setVisible(false);
				lbAddSize.setVisible(false);
				tfStartNumber.setVisible(false);
				tfAddSize.setVisible(false);
			}
		}
	}

	public int getFilesCount(File file) {
		File[] files = file.listFiles();
		return files.length;

	}

	public boolean isExists(String str) {
		File file = new File(str);
		return file.exists();
	}

	public void rename(String src) {

		int mode = cbSelcetModel.getSelectedIndex();

		File file = new File(src);

		File[] files = file.listFiles();

		for (int i = 0; i < files.length; i++) {
			if (rbReAllDirectory.isSelected()) {
				if (files[i].isDirectory()) {
					rename(files[i].toString());
				}
			}

			switch (mode) {
			case 0:
				if (rbToLower.isSelected()) {
					files[i].renameTo(new File(files[i].getParent() + "/"
							+ files[i].getName().toString().toUpperCase()));
				} else if (rbToUpper.isSelected()) {
					files[i].renameTo(new File(files[i].getParent() + "/"
							+ files[i].getName().toString().toLowerCase()));
				} else if (rbOneUpper.isSelected()) {
					files[i].renameTo(new File(files[i].getParent() + "/"
							+ files[i].getName().toString().toUpperCase()));
				}

				break;
			case 1:
				files[i].renameTo(new File(files[i].getParent() + "/"
						+ getFileName(files[i]) + "." + tfExt.getText().trim()));
				break;
			case 2:
				files[i].renameTo(new File(files[i].getParent()
						+ "/"
						+ getFileName(files[i]).replace(
								tfSource.getText().trim(),
								tfReplace.getText().trim())
						+ getSuffix(files[i])));
				break;
			case 3:
				if (rbNumber.isSelected()) {
					int start = Integer
							.parseInt(tfStartNumber.getText().trim());
					int add = Integer.parseInt(tfAddSize.getText().trim());

					start = start + i * add;
					if (rbPrefix.isSelected()) {
						files[i].renameTo(new File(files[i].getParent() + "/"
								+ start + tfPrefix.getText().trim()
								+ getFileName(files[i])
								+ tfSuffix.getText().trim()
								+ getSuffix(files[i])));
					} else if (rbSuffix.isSelected()) {
						files[i].renameTo(new File(files[i].getParent() + "/"
								+ tfPrefix.getText().trim()
								+ getFileName(files[i])
								+ tfSuffix.getText().trim() + start
								+ getSuffix(files[i])));
					} else if (rbPrefix.isSelected() && rbSuffix.isSelected()) {
						files[i].renameTo(new File(files[i].getParent() + "/"
								+ start + tfPrefix.getText().trim()
								+ getFileName(files[i])
								+ tfSuffix.getText().trim() + start
								+ getSuffix(files[i])));
					}

				} else {
					files[i].renameTo(new File(files[i].getParent() + "/"
							+ tfPrefix.getText().trim() + getFileName(files[i])
							+ tfSuffix.getText().trim() + getSuffix(files[i])));
				}
				break;

			default:
				break;
			}
			lbStatus.setText("更名" + i + "个文件");
		}

	}

	public String getSuffix(File file) {
		String name = file.getName();
		String ext = "";
		int i = name.lastIndexOf(".");
		if (i > 0) {
			ext = name.substring(i);

		}

		return ext;
	}

	public String getFileName(File file) {
		String name = file.getName();

		return name.replace(getSuffix(file), "");
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) {
			int index = cbSelcetModel.getSelectedIndex();
			switch (index) {
			case 0:

				setPanel(0);
				break;
			case 1:

				setPanel(1);
				break;
			case 2:

				setPanel(2);
				break;
			case 3:

				setPanel(3);
				break;

			default:
				break;
			}
		}

	}

	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		new changeFileName();

	}

}
